#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N = 1e7+5;

struct node{
	long long l, r, id, w;
	friend bool operator <(const node p1, const node p2){
		return p1.w > p2.w;
	}
}a[110];

char s[N];
long long cnt[53], cntNode = 0;
long long ans = 0;

int main(){
	scanf("%s", s);
	int lens = strlen(s);
	for(int i=0;i<lens;i++){
		if(s[i]>='a' && s[i]<='z'){
			cnt[s[i] - 'a']++;
		}else{
			cnt[s[i] - 'A' + 26]++;
		}
	}
	priority_queue<node> q;
	for(int i=0;i<=51;i++){
		if(cnt[i]==0){
			continue;
		}
		a[cntNode].l = a[cntNode].r = -1;
		a[cntNode].w = cnt[i];
		a[cntNode].id = cntNode;
		q.push(a[cntNode]);
		cntNode++;
	}
	int num = cntNode;
	for(int i=0;i<num-1;i++){
		node t1 = q.top(); q.pop();
		node t2 = q.top(); q.pop();
		a[cntNode].l = t1.id;
		a[cntNode].r = t2.id;
		a[cntNode].w = t1.w + t2.w;
//		printf("a[%d].w = %d + %d\n", cntNode, t1.w, t2.w);
		ans += a[cntNode].w;
		a[cntNode].id = cntNode;
		q.push(a[cntNode]);
		cntNode++;
	}
	if(num == 1){
		ans = 1;
	}
	printf("%lld\n", ans);
	return 0;
}
